package com.github.caijh.mall.common.validator;

import com.github.caijh.framework.web.threadlocal.ThreadLocalStore;
import com.github.caijh.mall.authorization.manager.ThreadLocalKeys;

public interface TokenRedisValidator extends TokenValidator {

    String getKey(String token);

    String getValue(String key);

    void setValue(String key, String value);

    @Override
    default boolean validate(String token) {
        try {
            String key = getKey(token);
            String value = getValue(key);
            boolean exists = false;
            if (value != null) {
                exists = true;
                ThreadLocalStore.put(ThreadLocalKeys.ID, value);
                setValue(key, value);
            }
            return exists;
        } catch (Exception e) {
            return false;
        }
    }

}
